<template>
  <view class="uni-container">
    <uni-forms ref="form" class="layerPublicStyle" :model="formData" validateTrigger="bind">

    <uni-forms-item name="remark" class="_margin_bottom10">
      <lay-form-item label="备注信息" class="_row_style">
            <lay-textarea  v-model="formData.remark" style="width: 300px;" :resize="true"></lay-textarea>
      </lay-form-item>
    </uni-forms-item>

    <uni-forms-item name="sn" class="_margin_bottom10">
      <lay-form-item label="编号SN"  class="_row_style">
        <div class="_content">
                {{  formData.sn }}
            </div>
      </lay-form-item>
    </uni-forms-item>

    <uni-forms-item name="key" class="_margin_bottom10">
      <lay-form-item label="识别码KEY"  class="_row_style">
        <div class="_content">
                {{  formData.key }}
            </div>
      </lay-form-item>
    </uni-forms-item>
    <uni-forms-item name="work_status" class="_margin_bottom10">
      <lay-form-item label="设备状态"  class="_row_style">
        <div class="_content">
                {{  options.work_status_valuetotext[formData.work_status]   }}
            </div>
      </lay-form-item>
    </uni-forms-item>
      <lay-form-item  class="_row_style" style="margin-top:20px">
        <lay-button @click="submit" type="normal">确认修改</lay-button>
        <lay-button @click="closeAll">取消关闭</lay-button>
      </lay-form-item>
    </uni-forms>
  </view>
</template>

<script>
  import { getAllMerchantList } from '@/utils/cloud.js'
  import { validator,enumConverter } from '../../js_sdk/validator/food-printer-list.js';
  import {store} from '@/uni_modules/uni-id-pages/common/store.js'

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'food-printer-list';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "merchant_id": store.userInfo.merchant_id,
        "sn": "",
        "key": "",
        "remark": "",
        "print_times": null,
        "success_times": null,
        "error_times": null
      }
      return {
        merchantList:[],

        options:{
          ...enumConverter
        },
        formData,
        formOptions: {

        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    mounted() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      init(id){
        console.log(id)
        this.formDataId = id
        this.getDetail(id)
      },
      closeAll(){
        this.$emit(`manualClose`,`addComponent`)
      },

      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {

          res.merchant_id =  store.userInfo.merchant_id
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.$emit(`refreshData`)
          this.closeAll();
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("work_status,sn,key,remark,print_times,success_times,error_times").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
